﻿using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Collections;

namespace LdExpress
{
    public class CarrDoucments
    {
        //INDEX + CARRIER + PORT + DOC + PT TUYEN + TEL + MOBILE + EMAIL + SKYPE + CUS + PT TUYEN + TEL + MOBILE + EMAIL + SKYPE
        public long ID = -1;
        public string Carr = "";
        public string Port = "";
        public string Doc = "";
        public string CarrPTTuyen = "";
        public string CarrTel = "";
        public string CarrMobile = "";
        public string CarrEmail = "";
        public string CarrSupport = "";

        public string Cust = "";
        public string CustPTTuyen = "";
        public string CustTel = "";
        public string CustMobile = "";
        public string CustEmail = "";
        public string CustSupport = "";

        public string Accnt = "";
        public string AccntPTTuyen = "";
        public string AccntTel = "";
        public string AccntMobile = "";
        public string AccntEmail = "";
        public string AccntSkype = "";

        public string Oper = "";
        public string OperPTTuyen = "";
        public string OperTel = "";
        public string OperMobile = "";
        public string OperEmail = "";
        public string OperSkype = "";

        public string DocImport = "";
        public string DocImportPTTuyen = "";
        public string DocImportTel = "";
        public string DocImportMobile = "";
        public string DocImportEmail = "";
        public string DocImportSkype = "";

        public string Description = "";
        public const int CarrSale = 0;  // Carr Contact
        public const int CarrCus = 1;   // Doc
        public const int CarrDoc = 2;   // Customer
        public const int CarrAccnt = 3; // Accounting
        public const int CarrOper = 4;  //Operation
        public const int CarrDocImport = 5;  //Carr Doc NK

        public int Sort = 0;
        public long LocationID = 0;
        public long CarrID = 0;

        public CarrDoucments()
        {
        }

        public CarrDoucments(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_carrdocuments";

            if (ID <= 0) // new object, we insert new record to database
            {
                strSQL = "Insert into " + strTableName + " (" +
                             "`Carr`" +
                             ",`Port`" +
                             ",`Doc`" +
                            ",`CarrPTTuyen`" +
                            ",`CarrTel`" +
                            ",`CarrMobile`" +
                            ",`CarrEmail`" +
                            ",`CarrSupport`" +
                            ",`Cust`" +
                            ",`CustPTTuyen`" +
                            ",`CustTel`" +
                            ",`CustMobile`" +
                            ",`CustEmail`" +
                            ",`CustSupport`" +
                             ",`Description`" +
                             ",`Sort`" +
                             ",`Accnt`" +
                             ",`AccntPTTuyen`" +
                             ",`AccntTel`" +
                             ",`AccntMobile`" +
                             ",`AccntEmail`" +
                             ",`AccntSkype`" +
                             ",`Oper`" +
                             ",`OperPTTuyen`" +
                             ",`OperTel`" +
                             ",`OperMobile`" +
                             ",`OperEmail`" +
                             ",`OperSkype`" +
                              ",`DocImport`" +
                             ",`DocImportPTTuyen`" +
                             ",`DocImportTel`" +
                             ",`DocImportMobile`" +
                             ",`DocImportEmail`" +
                             ",`DocImportSkype`" +
                             ",`LocationID`" +
                             ",`CarrID`" +
                             ") " +
                         " values(" +
                             "'" + DBModule.RefineString(Carr) + "'" +
                             ",'" + DBModule.RefineString(Port) + "'" +
                            ",'" + DBModule.RefineString(Doc) + "'" +
                                ",'" + DBModule.RefineString(CarrPTTuyen) + "'" +
                                ",'" + DBModule.RefineString(CarrTel) + "'" +
                                ",'" + DBModule.RefineString(CarrMobile) + "'" +
                                ",'" + DBModule.RefineString(CarrEmail) + "'" +
                                ",'" + DBModule.RefineString(CarrSupport) + "'" +
                                ",'" + DBModule.RefineString(Cust) + "'" +
                                ",'" + DBModule.RefineString(CustPTTuyen) + "'" +
                                ",'" + DBModule.RefineString(CustTel) + "'" +
                                ",'" + DBModule.RefineString(CustMobile) + "'" +
                                ",'" + DBModule.RefineString(CustEmail) + "'" +
                                ",'" + DBModule.RefineString(CustSupport) + "'" +
                             "," + "'" + DBModule.RefineString(Description) + "'" +
                             "," + Sort.ToString() +
                             "," + "'" + DBModule.RefineString(Accnt) + "'" +
                             "," + "'" + DBModule.RefineString(AccntPTTuyen) + "'" +
                             "," + "'" + DBModule.RefineString(AccntTel) + "'" +
                             "," + "'" + DBModule.RefineString(AccntMobile) + "'" +
                             "," + "'" + DBModule.RefineString(AccntEmail) + "'" +
                             "," + "'" + DBModule.RefineString(AccntSkype) + "'" +
                             "," + "'" + DBModule.RefineString(Oper) + "'" +
                             "," + "'" + DBModule.RefineString(OperPTTuyen) + "'" +
                             "," + "'" + DBModule.RefineString(OperTel) + "'" +
                             "," + "'" + DBModule.RefineString(OperMobile) + "'" +
                             "," + "'" + DBModule.RefineString(OperEmail) + "'" +
                             "," + "'" + DBModule.RefineString(OperSkype) + "'" +

                             "," + "'" + DBModule.RefineString(DocImport) + "'" +
                             "," + "'" + DBModule.RefineString(DocImportPTTuyen) + "'" +
                             "," + "'" + DBModule.RefineString(DocImportTel) + "'" +
                             "," + "'" + DBModule.RefineString(DocImportMobile) + "'" +
                             "," + "'" + DBModule.RefineString(DocImportEmail) + "'" +
                             "," + "'" + DBModule.RefineString(DocImportSkype) + "'" +
                              "," + LocationID.ToString() +
                              "," + CarrID.ToString() +
                             ")";
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("carrdocuments", "add", cn, trans);
                //GV.Response.Write(strSQL);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                            "Carr='" + DBModule.RefineString(Carr) + "'" +
                            ",Port='" + DBModule.RefineString(Port) + "'" +
                            ",Doc='" + DBModule.RefineString(Doc) + "'" +
                            ",CarrPTTuyen='" + DBModule.RefineString(CarrPTTuyen) + "'" +
                            ",CarrTel='" + DBModule.RefineString(CarrTel) + "'" +
                            ",CarrMobile='" + DBModule.RefineString(CarrMobile) + "'" +
                            ",CarrEmail='" + DBModule.RefineString(CarrEmail) + "'" +
                            ",CarrSupport='" + DBModule.RefineString(CarrSupport) + "'" +
                            ",Cust='" + DBModule.RefineString(Cust) + "'" +
                            ",CustPTTuyen='" + DBModule.RefineString(CustPTTuyen) + "'" +
                            ",CustTel='" + DBModule.RefineString(CustTel) + "'" +
                            ",CustMobile='" + DBModule.RefineString(CustMobile) + "'" +
                            ",CustEmail='" + DBModule.RefineString(CustEmail) + "'" +
                            ",CustSupport='" + DBModule.RefineString(CustSupport) + "'" +
                            ",Description='" + DBModule.RefineString(Description) + "'" +
                            ",Sort = " + Sort.ToString() +
                            ",Accnt='" + DBModule.RefineString(Accnt) + "'" +
                            ",AccntPTTuyen='" + DBModule.RefineString(AccntPTTuyen) + "'" +
                            ",AccntTel='" + DBModule.RefineString(AccntTel) + "'" +
                            ",AccntMobile='" + DBModule.RefineString(AccntMobile) + "'" +
                            ",AccntEmail='" + DBModule.RefineString(AccntEmail) + "'" +
                            ",AccntSkype='" + DBModule.RefineString(AccntSkype) + "'" +
                            ",Oper='" + DBModule.RefineString(Oper) + "'" +
                            ",OperPTTuyen='" + DBModule.RefineString(OperPTTuyen) + "'" +
                            ",OperTel='" + DBModule.RefineString(OperTel) + "'" +
                            ",OperMobile='" + DBModule.RefineString(OperMobile) + "'" +
                            ",OperEmail='" + DBModule.RefineString(OperEmail) + "'" +
                            ",OperSkype='" + DBModule.RefineString(OperSkype) + "'" +
                             ",DocImport='" + DBModule.RefineString(DocImport) + "'" +
                            ",DocImportPTTuyen='" + DBModule.RefineString(DocImportPTTuyen) + "'" +
                            ",DocImportTel='" + DBModule.RefineString(DocImportTel) + "'" +
                            ",DocImportMobile='" + DBModule.RefineString(DocImportMobile) + "'" +
                            ",DocImportEmail='" + DBModule.RefineString(DocImportEmail) + "'" +
                            ",DocImportSkype='" + DBModule.RefineString(DocImportSkype) + "'" +
                            ",LocationID = " + LocationID.ToString() +
                            ",CarrID = " + CarrID.ToString() +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("carrdocuments", "edit", cn, trans);
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {

            string strSQL = "DELETE FROM tbl_carrdocuments WHERE ID = " + this.ID.ToString();
            UserObjectsLog.SaveObjectActionLog("carrdocuments", "delete", cn, trans);
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_carrdocuments WHERE ID = " + ID.ToString();

            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Carr")) Carr = dr["Carr"].ToString();
                if (!dr.IsNull("Port")) Port = dr["Port"].ToString();
                if (!dr.IsNull("Doc")) Doc = dr["Doc"].ToString();
                if (!dr.IsNull("CarrPTTuyen")) CarrPTTuyen = dr["CarrPTTuyen"].ToString();
                if (!dr.IsNull("CarrTel")) CarrTel = dr["CarrTel"].ToString();
                if (!dr.IsNull("CarrMobile")) CarrMobile = dr["CarrMobile"].ToString();
                if (!dr.IsNull("CarrEmail")) CarrEmail = dr["CarrEmail"].ToString();
                if (!dr.IsNull("CarrSupport")) CarrSupport = dr["CarrSupport"].ToString();
                if (!dr.IsNull("Cust")) Cust = dr["Cust"].ToString();
                if (!dr.IsNull("CustPTTuyen")) CustPTTuyen = dr["CustPTTuyen"].ToString();
                if (!dr.IsNull("CustTel")) CustTel = dr["CustTel"].ToString();
                if (!dr.IsNull("CustMobile")) CustMobile = dr["CustMobile"].ToString();
                if (!dr.IsNull("CustEmail")) CustEmail = dr["CustEmail"].ToString();
                if (!dr.IsNull("CustSupport")) CustSupport = dr["CustSupport"].ToString();
                if (!dr.IsNull("Description")) Description = dr["Description"].ToString();
                if (!dr.IsNull("Sort")) Sort = int.Parse(dr["Sort"].ToString());
                if (!dr.IsNull("Accnt")) Accnt = dr["Accnt"].ToString();
                if (!dr.IsNull("AccntPTTuyen")) AccntPTTuyen = dr["AccntPTTuyen"].ToString();
                if (!dr.IsNull("AccntTel")) AccntTel = dr["AccntTel"].ToString();
                if (!dr.IsNull("AccntMobile")) AccntMobile = dr["AccntMobile"].ToString();
                if (!dr.IsNull("AccntEmail")) AccntEmail = dr["AccntEmail"].ToString();
                if (!dr.IsNull("AccntSkype")) AccntSkype = dr["AccntSkype"].ToString();

                if (!dr.IsNull("Oper")) Oper = dr["Oper"].ToString();
                if (!dr.IsNull("OperPTTuyen")) OperPTTuyen = dr["OperPTTuyen"].ToString();
                if (!dr.IsNull("OperTel")) OperTel = dr["OperTel"].ToString();
                if (!dr.IsNull("OperMobile")) OperMobile = dr["OperMobile"].ToString();
                if (!dr.IsNull("OperEmail")) OperEmail = dr["OperEmail"].ToString();
                if (!dr.IsNull("OperSkype")) OperSkype = dr["OperSkype"].ToString();

                if (!dr.IsNull("DocImport")) DocImport = dr["DocImport"].ToString();
                if (!dr.IsNull("DocImportPTTuyen")) DocImportPTTuyen = dr["DocImportPTTuyen"].ToString();
                if (!dr.IsNull("DocImportTel")) DocImportTel = dr["DocImportTel"].ToString();
                if (!dr.IsNull("DocImportMobile")) DocImportMobile = dr["DocImportMobile"].ToString();
                if (!dr.IsNull("DocImportEmail")) DocImportEmail = dr["DocImportEmail"].ToString();
                if (!dr.IsNull("DocImportSkype")) DocImportSkype = dr["DocImportSkype"].ToString();

                if (!dr.IsNull("LocationID")) LocationID = long.Parse(dr["LocationID"].ToString());
                if (!dr.IsNull("CarrID")) CarrID = long.Parse(dr["CarrID"].ToString());
            }
        }

        public static void GetList(string strIDs, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select tbl_carrdocuments.*,TBLocation.Name as LocationName  from tbl_carrdocuments " +
                  " left join tbl_Location TBLocation on tbl_carrdocuments.LocationID = TBLocation.ID " +
                  " where 1=1 ";
            if ((strIDs != "") && (strIDs != null))
            {
                if (strIDs != "All")
                    strSQL += strIDs;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            //GV.Response.Write("sql=" + strSQL);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        public static void DoSearch(string Condition, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_carrdocuments where 1=1 ";
            if (Condition.Length > 0)
                strSQL += Condition;
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
    }
}
